$("#sumbit").click(function () {
    var url = "http://localhost:8080/login";
    var userName = $("#loginName").val();
    var password = $("#password").val();

    console.info("账号 " + userName);
    $.ajax({
        type: "POST",
        url: url,
        data: {
            userName: userName,
            password: password
        },
        success: function (data) {
            console.info(data);
            if (data.result) {
                window.location.href = "http://localhost:8080/html/city.html";
            } else {
                alert("登陆失败");
            }
        },
        dataType: "json"
    })
});

$("#search").click(function () {
    var url = "http://localhost:8080/getShortWay";
    var start = $("#start").val();
    var end = $("#end").val();
    $.ajax({
        type: "POST",
        url: url,
        data: {
            startCity: start,
            endCity: end
        },
        success: function (data) {
            console.info(data);
            var value = "<font size='5'>";
            value += "最短路径长度为: " + data.result.extent + "<br>";
            value += " 所经过的路为: <br>";

            $.each(data.result.ways, function (i, temp) {
                value += temp.cityName + " "
            });
            value += "<br>";
            value += "城市之间的距离为: <br>";

            $.each(data.result.cityDistances, function (i, temp) {
                value += temp.fromCity.cityName + " 到 " + temp.toCity.cityName + " 的长度为：";
                if (temp.distance >= 100000) {
                    value += "路径不可达";
                } else {
                    value += temp.distance;
                }


                value += "<br>";
            });
            value += "</font>";
            $("#value").html(value);

        },
        dataType: "json"
    })

});

$("#registered").click(function () {
    var url = "http://localhost:8080/registered";
    var userName = $("#loginName").val();
    var password = $("#password").val();
    var isPassword = $("#isPassword").val();

    if(password !== isPassword){
        alert("两次密码不同，请重新输入")
    }

    $.ajax({
        type:"POST",
        url:url,
        data:{
            userName:userName,
            password:password
        },
        dataType:"json",
        success:function (data) {
            if(data.result === false){
                alert("注册失败");
            }else{
                window.location.href = "http://localhost:8080/html/city.html";
            }
        }
    })

});